home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 September (IDG) / Sep99.iso / Shareware World / Utilities / Text Processing / Alpha / Tcl / Modes / latex Mode / latex.tcl next >
Encoding:
Text File  |  1999-05-21  |  14.9 KB  |  418 lines  |  [TEXT/ALFA]

  1. #############################################################################
  2. #############################################################################
  3. #
  4. # latex.tcl, version 4.6:  macros, menu, and key bindings for LaTeX users
  5. #
  6. #############################################################################
  7. #
  8. # version 1.1 and 1.2 (11/10/92) by Richard T. Austin <austin@eecs.umich.edu>
  9. # versions 2.0--3.2 and 3.2t (3/97) by Tom Scavo <trscavo@syr.edu>
  10. # versions 4.0 onwards (9/97) by Vince Darley <darley@fas.harvard.edu>
  11. # If you make improvements to this file, please share them!
  12. #
  13. # LaTeX 2.09 is no longer supported.  If someone wants to make the 
  14. # necessary changes to support it, they are welcome.
  15. #############################################################################
  16. #############################################################################
  17.  
  18. # Initialize TeX mode:
  19. alpha::mode TeX 4.7.0 texMenu {
  20.     *.tex *.ltx *.dtx *.ins *.cls *.clo *.sty *.log *.aux *.bbl *.idx 
  21.     *.ind *.glo *.gls *.toc *.lof *.lot *.blg *.ilg *.fd *.drv *.fdd *.def
  22. } { 
  23.     texMenu electricReturn electricTab
  24. } { 
  25.     addMenu texMenu "•270" TeX
  26.     set unixMode(latex) {TeX}
  27.     
  28.     alpha::package require -loose Alpha 7.1
  29.     # Reset the search path so it will be rebuilt the next time it's needed:
  30.     set AllTeXSearchPaths {}
  31.     
  32.     # Initialize TeX-related application signatures:
  33.     foreach app {tex viewDVI printDVI dvips viewPDF viewPS printPS bibtex makeindex} {
  34.     ensureset ${app}Sig {}
  35.     }
  36.     
  37.     # Install the short LaTeX menu, by default:
  38.     ensureset useShortLaTeXMenu 1
  39.     
  40.     # Do not load AMS-LaTeX, by default:
  41.     ensureset useAMSLaTeX 0
  42.     
  43.     # To add a new fileset type, all we have to do is this:
  44.     set fileSetsTypesThing(tex) "tex"
  45.     lappend fileSetsTypes "tex"
  46. } maintainer {
  47.     "Vince Darley" darley@fas.harvard.edu <http://www.fas.harvard.edu/~darley/>
  48. } uninstall {this-directory} help {file "LaTeX Help"}
  49.  
  50. #############################################################################
  51. #
  52. # Autoload procedures
  53. #
  54. #############################################################################
  55.  
  56. proc dummyTeX {} {}
  57. proc texMenu {} {}
  58.  
  59. # Update this preference:
  60. if {[info exists TeXmodeVars(TeXInputsFolder)]} {
  61.     alertnote "The 'TeXInputsFolder' pref has been replaced by\
  62.       'TeXSearchPath', which is now a list of folders to search.\
  63.       I'll copy over your preference and delete the old one."
  64.     lappend TeXmodeVars(TeXSearchPath) $TeXmodeVars(TeXInputsFolder)
  65.     unset TeXmodeVars(TeXInputsFolder)
  66.     lappend modifiedArrayElements [list TeXInputsFolder TeXmodeVars] \
  67.       [list TeXSearchPath TeXmodeVars]
  68. }
  69.  
  70. #############################################################################
  71. #
  72. # Flags and variables
  73. #
  74. #############################################################################
  75.  
  76. # Flags ---
  77.  
  78. # If a particular template requires a certain package, warn if it's not there.
  79. newPref f warnIfPackageNotIncluded 1 TeX
  80. # Build the submenu of all known packages.  Can be slow.
  81. newPref f buildPkgsSubmenu 0 TeX
  82. newPref f deleteEnvNoisily 1 TeX
  83. newPref f deleteObjNoisily 1 TeX
  84. # Typeset occurs in background by default (you can always override with
  85. # a different key-combination
  86. newPref f runTeXInBack 0 TeX makeProcessMenu
  87. # When your actions require the mode to search for something, 'beep'
  88. # if the search fails (otherwise just post a silent message).
  89. newPref f searchNoisily 1 TeX
  90. # Turn '...' into '\ldots' automatically as you type
  91. newPref f smartDots 1 TeX
  92. # Turn ' or " into ` or `` or ' or '' automatically as you type, depending
  93. # upon the context
  94. newPref f smartQuotes 1 TeX
  95. newPref f useBrackets 0 TeX
  96. # Use '$...$' and '$$...$$' for mathematics rather than the LaTeX
  97. # expressions
  98. newPref f useDollarSigns 0 TeX shadowUseDollarSigns
  99. newPref f wordWrap 1 TeX
  100.  
  101. # names of commands which insert boxed graphics
  102. newPref v boxMacroNames {includegraphics includegraphics*} TeX shadowBoxMacroNames
  103. # commands used by your citation package
  104. newPref v citeCommands {cite nocite citet citeauthor citep citeyear} TeX shadowCiteRefCommands
  105. # the list of known mathematical environments
  106. newPref v mathEnvironments [list "math" "displaymath" "equation" "eqnarray" "eqnarray*" "array"] TeX
  107. newPref v fillColumn {70} TeX
  108. newPref v funcExpr {\\(sub)*section(\[.*\]|\*)?({[^{}]*})?} TeX
  109. newPref v parseExpr {[^\{]*\{(.*)\}[^\}]*} TeX
  110. newPref v funcExprAlt {\\section(\[.*\]|\*)?({[^{}]*})?} TeX 
  111. newPref v leftFillColumn {0} TeX
  112. newPref v prefixString {% } TeX
  113. # the commands which are used to refer to labels elsewhere in the text
  114. newPref v refCommands {ref eqref pageref vref vpageref} TeX shadowCiteRefCommands
  115. newPref v wrapBreak {(\w+|\\(\\\*?|[^A-Za-z\t\r*\s\]|[A-Za-z]+\*?))} TeX
  116. newPref v wrapBreakPreface {([^\w\]|.\\)} TeX
  117. newPref v wordBreak {(([\w:-])+|\\(\\|[a-zA-Z]+)\*?|\\[^a-zA-Z*\s])} TeX
  118. if {[info tclversion] < 8.0} {
  119.     newPref v wordBreakPreface "(\[^-\\w:\\\]|.\\\\)" TeX
  120. } elseif {[info tclversion] < 8.1} {
  121.     newPref v wordBreakPreface "(\[^-\\w:\\\]|.\\\\|^|\$)" TeX
  122. } else {
  123.     newPref v wordBreakPreface "(\[^-\\w:\\\\\]|.\\\\)" TeX
  124. }
  125.  
  126. set texParaCommands {\[|\]|begin|end|(protect\\)?label|(sub)*section\*?|subfigure|paragraph|centerline|centering|caption|chapter|item|bibitem|intertext|(protect\\)?newline|includegraphics\*?}
  127. # Where your TeX application searches for input files
  128. newPref v TeXSearchPath "" TeX shadowTeXInputs
  129. # The delimiter to use by default in labels inserted into the text.
  130. # For instance the default is a colon, so labels are written 
  131. # \label{fig:...} or \label{eq:...}.  You may prefer a hyphen '-' or...
  132. newPref v standardTeXLabelDelimiter ":" TeX \
  133.   {hook::callAll TeX::labelDelimChanged *}
  134.  
  135. # Pressing this key will insert a subscript _{} if the context is relevant
  136. newPref binding smartSubscripts "/_<U" TeX "" 1
  137. # Pressing this key will insert a superscript _{} if the context is relevant
  138. newPref binding smartSuperscripts "/^<U" TeX "" 1
  139. # If possible, show TeX log inside Alpha (doesn't work with all TeX apps)
  140. newPref variable showTeXLog 1 TeX "" \
  141.   [list "Never" "Only after error" "Always"] index
  142. # Chooses between using the Marks menu for a list of TeX commands or
  143. # the structural content of the file.
  144. newPref variable markCommandsNotStructure \
  145.   [list .sty .dtx .cls .clo .def .cfg .fd] TeX
  146. # Don't indent lines between \begin,\end pairs. This only works for
  147. # automatic indentation after you press return or tab, not for blocks
  148. # of code inserted on the fly by code templates for example.
  149. newPref flag dontIndentBeginEnd 0 TeX
  150. # Take over entire numeric keypad with dummy bindings.  Helps to avoid
  151. # bad keypresses
  152. newPref flag takeOverNumericKeypad 1 TeX
  153.  
  154. #############################################################################
  155. #
  156. # Initialization
  157. #
  158. #############################################################################
  159.  
  160. hook::register activateHook TeX::activate TeX
  161. hook::register saveasHook makeProcessMenu TeX
  162.  
  163. proc makeProcessMenu {args} {
  164.     menu::buildSome Process
  165. }
  166.  
  167. proc TeX::activate {name} {
  168.     global TeX::tmpFile PREFS
  169.     if {[info exists TeX::tmpFile($name)]} {
  170.     if {[regexp "^[file join ${PREFS} tmp .*]" $name]} {
  171.         # It was a temporary file
  172.         message "$name is temporary"
  173.         # Send a message to myself, to arrive after the window
  174.         # has been completely opened (else killWindow will crash Alpha).
  175.         dosc -c 'ALFA' [list TeX::fromTempToReal $name]
  176.     }
  177.     } else {
  178.     makeProcessMenu
  179.     }
  180. }
  181.  
  182. proc TeX::fromTempToReal {name} {
  183.     global TeX::tmpFile
  184.     # Bring temp window to front, find cursor position and close the window.
  185.     bringToFront $name
  186.     set tempRowCol [posToRowCol [set tempPos [getPos]]]
  187.     killWindow
  188.     # Now get the real window, and go to the correct position.
  189.     if {[info exists TeX::tmpFile($name)]} {
  190.     set item [set TeX::tmpFile($name)]
  191.     unset TeX::tmpFile($name)
  192.     switch -- [lindex $item 0] {
  193.         "clipboard" {
  194.         alertnote "There is no original file to edit; you were\
  195.           typesetting the clipboard."
  196.         return
  197.         }
  198.         "selection" {
  199.         set file [lindex $item 1]
  200.         set lineOffset [lindex $item 2]
  201.         bringToFront $file
  202.         goto [rowColToPos [expr {$lineOffset + [lindex $tempRowCol 0]}] [lindex $tempRowCol 1]]
  203.         message "Editing on given TeX line"
  204.         return
  205.         }
  206.     }
  207.     }
  208.     alertnote "Couldn't find original file!"
  209. }
  210.  
  211. # registered only if we're using Textures
  212. proc TexturescloseHook {name} {
  213.     global Texturesconnections
  214.     if {[info exists Texturesconnections]} {
  215.     set winNames [map car $Texturesconnections]
  216.     if { [set pos [lsearch -exact $winNames $name]] > -1 } {
  217.         # Close Textures' connection:
  218.         global texAppSignatures
  219.         set TeXjob [lindex [lindex $Texturesconnections $pos] 1]
  220.         AEBuild -r '$texAppSignatures(Textures)' BSRs Disc Jobi $TeXjob
  221.         # Update list of connections:
  222.         set Texturesconnections [lreplace $Texturesconnections $pos $pos]
  223.     }
  224.     }
  225. }
  226.  
  227. proc TexturesLaunched {args} {
  228.     global Texturesconnections
  229.     set Texturesconnections ""    
  230. }
  231.  
  232. # The proc 'shadowtexSig' is executed automatically whenever 
  233. # the global variable 'texSig' is altered, say, by choosing
  234. # 'removedSavedSetting' on the Config menu.  Alpha now has
  235. # a nifty proc to handle all the hard work for us: (Vince)
  236. proc shadowtexSig {args} {
  237.     global packagesSubmenuItems AllTeXSearchPaths texSig texAppSignatures
  238.     # Reset the Packages submenu so that it will be rebuilt:
  239.     set packagesSubmenuItems ""
  240.     message {Building LaTeX menu…}
  241.     buildLaTeXMenuQuietly
  242.     # Reset the search path so it will be rebuilt the next time it's needed:
  243.     set AllTeXSearchPaths {}
  244.     # Set things up for Textures
  245.     if {[info exists texAppSignatures(Textures)] && \
  246.       $texSig == $texAppSignatures(Textures)} {
  247.         hook::register launch TexturesLaunched $texAppSignatures(Textures)
  248.         hook::register closeHook TexturescloseHook TeX
  249.     } else {
  250.         hook::deregister launch TexturesLaunched $texAppSignatures(Textures)
  251.         hook::deregister closeHook TexturescloseHook TeX
  252.     }
  253.     
  254. }
  255.  
  256. proc shadowTeXInputs {args} {
  257.     global packagesSubmenuItems AllTeXSearchPaths
  258.     
  259.     # Reset the Packages submenu so that it will be rebuilt:
  260.     set packagesSubmenuItems ""
  261.     
  262.     message "Rebuilding the Documents submenu…"
  263.     menu::buildSome Documents
  264.     message ""
  265.     
  266.     # Reset the search path so it will be rebuilt the next time it's needed:
  267.     set AllTeXSearchPaths {}
  268. }
  269.  
  270. # The proc 'shadowUseDollarSigns' is executed automatically whenever 
  271. # the flag 'useDollarSigns' is altered.  The proc 'mathModeSubmenu'
  272. # is defined in latexMenu.tcl.
  273. proc shadowUseDollarSigns {args} {
  274.     TeX::sub::MathModes
  275.     menu::buildSome "Math Modes"
  276. }
  277.  
  278. # we indent to previous non-comment line, +- a bit for \begin or \end
  279. proc TeX::indentLine {} {
  280.     set beg [lineStart [getPos]]
  281.     set text [getText $beg [nextLineStart $beg]]
  282.     regexp "^\[ \t\]*" $text white
  283.     set next [pos::math $beg + [string length $white]]
  284.     set end [pos::math $next + 5]
  285.     if {[pos::compare $end > [maxPos]]} {set end [maxPos]}
  286.     set lwhite [TeX::correctIndentation [getPos] [getText $next $end]]
  287.     
  288.     set lwhite [text::indentOf $lwhite]
  289.     if {$white != $lwhite} {
  290.     replaceText $beg $next $lwhite
  291.     }
  292.     goto [pos::math $beg + [string length $lwhite]]
  293. }
  294.  
  295. proc TeX::correctIndentation {pos {next ""}} {
  296.     global indentationAmount commentsArentSpecialWhenIndenting dontIndentBeginEnd
  297.     set beg [lineStart $pos]
  298.  
  299.     # find last non-empty, non-comment line
  300.     if {$commentsArentSpecialWhenIndenting} {
  301.     if {[catch {search -s -f 0 -r 1 -i 0 -m 0 -- "^\[ \t\]*\[^ \t\n\r\]" [pos::math $beg - 1]} lst]} {
  302.         return 0
  303.     }
  304.     } else {
  305.     if {[catch {search -s -f 0 -r 1 -i 0 -m 0 -- "^\[ \t\]*\[^ \t\n\r%\]" [pos::math $beg - 1]} lst]} {
  306.         return 0
  307.     }
  308.     }
  309.     
  310.     set line [getText [lindex $lst 0] [pos::math [nextLineStart [lindex $lst 0]] - 1]]
  311.     set lwhite [posX [pos::math [lindex $lst 1] - 1]]    
  312.     if {!$dontIndentBeginEnd} {
  313.     incr lwhite [expr {$indentationAmount * \
  314.       ([regexp {^[ \t]*\\begin\{} $line] - [regexp {^\\end\{} $next])}]
  315.     }
  316.     # only happens with poorly formatted files.
  317.     if {$lwhite < 0} {return 0} else {return $lwhite}
  318. }
  319.  
  320. #############################################################################
  321. #
  322. # Colorization
  323. #
  324. #############################################################################
  325.  
  326. newPref v keywordColor blue TeX
  327. newPref v sectionColor magenta TeX
  328. newPref v commentColor red TeX
  329. newPref v bracesColor green TeX
  330.  
  331. # The proc 'shadowCiteRefCommands' is executed whenever the variables 
  332. # TeXmodeVars(citeCommands) or TeXmodeVars(refCommands) are changed.
  333. proc shadowCiteRefCommands {args} {
  334.     global TeXmodeVars
  335.     set LaTeXClickWords {}
  336.     # insure that 'citeCommands' contains "cite" and "nocite":
  337.     lunion TeXmodeVars(citeCommands) cite nocite
  338.     foreach word $TeXmodeVars(citeCommands) { 
  339.     lappend LaTeXClickWords "\\\\$word" 
  340.     }
  341.     # insure that 'refCommands' contains "ref" and "pageref":
  342.     lunion TeXmodeVars(refCommands) ref pageref
  343.     foreach word $TeXmodeVars(refCommands) { 
  344.     lappend LaTeXClickWords "\\\\$word" 
  345.     }
  346.     # color and underline command-double-clickable LaTeX commands:
  347.     regModeKeywords -a -u -k $TeXmodeVars(keywordColor) TeX $LaTeXClickWords
  348. }
  349.  
  350. # The proc 'shadowBoxMacroNames' is executed whenever the variable 
  351. # TeXmodeVars(boxMacroNames) is changed.
  352. proc shadowBoxMacroNames {args} {
  353.     global TeXmodeVars
  354.     set LaTeXClickWords {}
  355.     foreach word $TeXmodeVars(boxMacroNames) { 
  356.     lappend LaTeXClickWords "\\\\$word" 
  357.     }
  358.     # color and underline command-double-clickable LaTeX commands:
  359.     regModeKeywords -a -u -k $TeXmodeVars(keywordColor) TeX $LaTeXClickWords
  360. }
  361.  
  362. proc colorLaTeXClickCommands {} {
  363.     global TeXmodeVars
  364.     lappend LaTeXClickWords \\documentclass \\usepackage \
  365.       \\input \\include \\InputIfFileExists \
  366.       \\bibliography \\bibliographystyle \
  367.       \\LoadClass \\RequirePackage
  368.     # color and underline command-double-clickable LaTeX commands:
  369.     regModeKeywords -a -u -k $TeXmodeVars(keywordColor) TeX $LaTeXClickWords
  370.     shadowCiteRefCommands
  371.     shadowBoxMacroNames
  372. }
  373.  
  374. proc colorLaTeXCommands {} {
  375.     global TeXmodeVars 
  376.     set LaTeXKeyWords {}
  377.     # Color comments, braces and dollar signs, and all LaTeX commands:
  378.     regModeKeywords -i "\}" -i "\{" -i "\$" -I $TeXmodeVars(bracesColor) \
  379.       -e {%} -m {\\} -c $TeXmodeVars(commentColor) \
  380.       -k $TeXmodeVars(keywordColor) TeX $LaTeXKeyWords
  381.     # Color sectioning commands:
  382.     regModeKeywords -a -k $TeXmodeVars(sectionColor) TeX \
  383.       { \\part \\chapter \\section \\subsection \\subsubsection \\paragraph }
  384. }
  385.  
  386. colorLaTeXCommands; rename colorLaTeXCommands ""
  387. colorLaTeXClickCommands; rename colorLaTeXClickCommands ""
  388.  
  389. #############################################################################
  390. #
  391. # LaTeX initialisation
  392. #
  393. #############################################################################
  394.  
  395. if {[alpha::tryToLoad "Initialising LaTeX" \
  396.   latexEngine.tcl {} \
  397.   latexComm.tcl {}\
  398.   latexMacros.tcl {}\
  399.   latexMenu.tcl {} \
  400.   latexKeys.tcl {} \
  401.   bindLaTeXKeys {Binding LaTeX keys} \
  402.   [list app::registerMultiple tex [list OTEX *TEX *XeT MPS*] \
  403.   [list •266 •267 •272 •299] shadowtexSig] {Building LaTeX menu}]} {
  404.     rename bindLaTeXKeys ""
  405. }
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.